package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.annotation.MultiRequestBody;
import com.woniuxy.resultCode.Result;
import com.woniuxy.resultCode.ResultCode;
import com.woniuxy.system.entity.PerformancePost;
import com.woniuxy.system.service.PerformancePostService;
import com.woniuxy.system.view.PerformancePostView;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 岗位管理 前端控制器
 * </p>
 *
 * @author woniuxy
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/system/performancePost")
@Slf4j
@RequiresAuthentication
public class PerformancePostController {
    @Autowired
    PerformancePostService performancePostService;

    @ApiOperation("获取所有岗位管理")
    @PostMapping("/getPost")
    public Result list(@MultiRequestBody(required = false)
                               PerformancePostView performancePostView,@MultiRequestBody(required = false)  String userid) {
        log.info("获取参数performancePostView:{},userid{}",performancePostView, userid);
        if(StringUtils.checkValNull(performancePostView.getCurrentPage())&&StringUtils.checkValNull(performancePostView.getPageSize())){
            List<PerformancePost> performancePostTree = performancePostService.getPerformancePostTree();
            return Result.ok().data("msg",performancePostTree);
        }
        QueryWrapper<PerformancePost> wrapper = getWrapper(performancePostView);
        Page<PerformancePost> page = new Page<>(performancePostView.getCurrentPage(),performancePostView.getPageSize());
        Page<PerformancePost> result = performancePostService.getAllPerformancePost(page,wrapper);
        List<PerformancePost> records = result.getRecords();
        long total = result.getTotal();
        long totalPages = result.getPages();
        return Result.ok().data("msg", records).data("total",total).data("totalPages",totalPages);
    }

    @ApiOperation("新增岗位")
    @PostMapping("/add")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result add(@MultiRequestBody PerformancePost performancePost,@MultiRequestBody(required = false) String userid) {
        boolean flag = performancePostService.addPerformancePost(performancePost,userid);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation("通过id删除岗位管理")
    @DeleteMapping("/delete/{id}")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result deleteById(@PathVariable Long id) {
        boolean flag = performancePostService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error()
                    .code(ResultCode.DELETE_ERROR
                            .getCode())
                    .message(ResultCode.DELETE_ERROR
                            .getMessage());
        }
    }

    @ApiOperation("更新岗位")
    @PostMapping("/update")
    @RequiresRoles(value={"1","2","3"},logical = Logical.OR)
    public Result update(@RequestBody PerformancePost performancePost) {
        boolean flag = performancePostService.updateById(performancePost);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    //封装queryWrapper
    private QueryWrapper<PerformancePost> getWrapper(PerformancePostView performancePostView) {
        QueryWrapper<PerformancePost> queryWrapper = new QueryWrapper<>();
        if (performancePostView != null) {
            if (StringUtils.isNotBlank(performancePostView.getName())) {
                queryWrapper.like("p.name", performancePostView.getName());
            }

        }
        return queryWrapper;
    }
}

